package org.bdware.irs.backend.utils;

import java.util.Arrays;

public class DOIParser {

    public static String ADMIN = "dou.ADMIN";


    public static String getAdminDOI(String doi){
        String prefix = getPrefix(doi);
        return prefix + "/" + ADMIN;
    }

    public static String getPrefix(String doi){
        String[] splits = doi.split("/");
        return splits[0];
    }

    public static String getParentPrefix(String doi){
        String prefix = getPrefix(doi);
        String[] prefixes = prefix.split("[.]");
        if(prefixes.length == 1)
            return prefix;
        String[] parents = Arrays.copyOfRange(prefixes, 0, prefixes.length - 1);
        return String.join(".",parents);
    }

    public static String getSuffix(String doi){
        if(!doi.contains("/"))
            return null;
        String[] splits = doi.split("/");
        return splits[1];
    }

    //根据doi的编码方式，返回doi对应的类型
    public static String getType(String doi){
        String suffix = getSuffix(doi);
        //只包含前缀，不包含后缀
        if(suffix == null || suffix.equals(""))
            return "prefix";
        //registry管理员
        if(suffix.equals("dou.ADMIN"))
            return "irs_admin";
        String[] splits = suffix.split("[.]");
        if(splits[0].equals("doip")){
            return "doip";
        }else if(splits[0].equals("dou")){
          return "dou";
        } else if(splits[0].equals("do")){
            return "do";
        }
        return "uwk";
    }

    public static void main(String[] args){
        System.out.println(getParentPrefix("test/liuyi"));
    }
}
